#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import json
import os

        
def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--exp_id', type=str, default='exp2')
    parser.add_argument('--n_ptl_iters', type=int, default=6)
    parser.add_argument('--suffix', type=str, default='')
    
    return parser.parse_args()   


def main(opt):
    # Det result root
    result_root = os.path.join("../results", opt.exp_id, "det")   
    
    # Collect results
    for test_dataset_type in ["vis", "oku", "icg"]:
        for acc_type in ["AP", "AP50"]:
            for ptl_iter in range(opt.n_ptl_iters):
                with open(os.path.join(result_root, "iter_%d" % ptl_iter, test_dataset_type, "results.json")) as f_in:
                    result = json.load(f_in)
                with open(os.path.join(result_root, "results.txt"), "a") as f_out:        
                    if ptl_iter == 0:
                        f_out.write("\n")
                        f_out.write("%s-%s" % (test_dataset_type, acc_type))
                    f_out.write("\n")
                    f_out.write("{:.3f}".format(float(result['bbox'][acc_type])))


if __name__ == "__main__":
    opt = parse_opt()
    main(opt)